package com.microframework.boot.visual.controller;

import java.sql.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jfinal.kit.StrKit;
import com.microframework.base.core.util.SecurityUtils;
import com.microframework.base.core.util.UuidUtil;
import com.microframework.base.web.page.PageQuery;
import com.microframework.base.web.resp.R;
import com.microframework.boot.visual.model.domain.MaterialCategory;
import com.microframework.boot.visual.service.IMaterialCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/visual/material")
@Tag(name = "素材管理", description = "素材管理相关接口")
public class MaterialCategoryController {
	
    @Autowired
    private IMaterialCategoryService materialCategoryService;
    
    @PostMapping("/load/all")
    @Operation(summary = "一次性加载素材树型数据", description = "一次性加载素材树型数据")
    public R<List<MaterialCategory>> loadAll(@RequestBody PageQuery<MaterialCategory> pageQuery) {
        return R.ok(materialCategoryService.loadAll(pageQuery));
    }
    
    @Operation(summary = "保存素材")
	@Parameters({ @Parameter(name = "materialCategory", description = "素材", required = true) })
	@PostMapping("/save")
	public R<MaterialCategory> save(HttpServletRequest request, @RequestBody MaterialCategory materialCategory) {
		try {
			if (StrKit.notBlank(materialCategory.getId())) {
				materialCategory.setModifyTime(new Date(System.currentTimeMillis()));
				materialCategory.setModifyUserId(SecurityUtils.getUserId());
				if (materialCategory.update()) {
					return R.ok(materialCategory);
				} else {
					return R.fail("更新失败.");
				}
			} else {
				materialCategory.setId(UuidUtil.getUUID());
				materialCategory.setCreateTime(new Date(System.currentTimeMillis()));
				materialCategory.setCreateUserId(SecurityUtils.getUserId());
				if (materialCategory.save()) {
					return R.ok(materialCategory);
				} else {
					return R.fail("保存失败.");
				}
			}
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}
    
	@Operation(summary = "删除素材")
	@Parameters({ @Parameter(name = "id", description = "素材id", required = true) })
	@DeleteMapping("/detain/{id}")
	public R<String> deleteById(@PathVariable("id") String id) {
		try {
			MaterialCategory org=new MaterialCategory();
			org.setId(id);
			org.setIsDel("1");
			if(org.update()) {
				return R.ok("删除成功.");
			}else {
				return R.fail("删除失败.");
			}
		}catch (Exception e) {
			log.error("删除异常:{}",e.getMessage());
			return R.fail(e.getMessage());
		}
	}
	
	
}
